package com.wn.search.service.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.wn.common.model.dto.BorrowSearchDTO;
import com.wn.search.bean.BorrowVO;
import com.wn.search.bean.Page;
import com.wn.search.service.IBorrowService;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.stereotype.Service;

/**
 * @author yellow Docter
 * @date 2023 -08-02
 * @desc
 */
@Service
public class IBorrowServiceImpl implements IBorrowService {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Override
    public Page searchBorrowRecord(BorrowSearchDTO borrowSearchDTO) {

        //01 构建  query
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();

        List<QueryBuilder> queryBuildersList =new ArrayList<>();
        //设置 查询条件
        // 通过name 的条件查询
        if (StringUtils.isNotBlank(borrowSearchDTO.getName())){
            QueryBuilder name = QueryBuilders.matchQuery("name", borrowSearchDTO.getName());
            queryBuildersList.add(name);
        }
        if (StringUtils.isNotBlank(borrowSearchDTO.getDescription())){
            QueryBuilder description = QueryBuilders.matchQuery("description", borrowSearchDTO.getDescription());
            queryBuildersList.add(description);
        }
        //搜索期限
        Integer startPeriod = borrowSearchDTO.getStartPeriod();
        if (startPeriod != null){
            QueryBuilder period = QueryBuilders.rangeQuery("period").gte( borrowSearchDTO.getStartPeriod());
            queryBuildersList.add(period);
        }

        // should() 或者    设置 多个查询条件
        queryBuilder.should().addAll(queryBuildersList);

        Query query = new NativeSearchQuery(queryBuilder);

        //设置分页
        query.setPageable(PageRequest.of(
                borrowSearchDTO.getPageNo(),
                borrowSearchDTO.getPageSize()));

        SearchHits<BorrowVO> searchHits = elasticsearchRestTemplate.search(query, BorrowVO.class);
        //02 search 搜索

        //总数
        long totalHits = searchHits.getTotalHits();

        //搜索后的 结果数据
        List<BorrowVO> borrowVOS = searchHits.stream()
                .map(searchHit -> searchHit.getContent())
                .collect(Collectors.toList());

        //03 封装 数据  Page
        Page page = new Page();
        //搜索的数据  存放集合
        page.setContent(borrowVOS);
        page.setPageNO(borrowSearchDTO.getPageNo());
        page.setPageSize(borrowSearchDTO.getPageSize());
        page.setTotal(totalHits);

        return page;
    }
}
